﻿

/*
* Project: HOERP
* Copyright(c)
* Version: 1.0.0 
* Author: Caijianfeng <sharewind@gmail.com> 
* DateTime: 2011年5月14日 星期六 上午 10:36:26
*/



package com.serp.dao.xt;

import org.tulip.ProjectLog;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.tulip.DaoException;
import org.tulip.RootDao;
import org.tulip.util.BeanUtil;

/**
 * 客户Dao
 */
public class CustomDao extends RootDao {
	/**
	 * Logger for this class
	 */
	private static final ProjectLog logger = ProjectLog.getLogger(CustomDao.class);

	@Override
	protected Object convert(ResultSet rs, List list) throws SQLException {
		Custom current = new Custom();
		if (list.contains("CUST_ID"))
			current.setCustId(rs.getLong("CUST_ID"));
		if (list.contains("CUST_NAME"))
			current.setCustName(rs.getString ("CUST_NAME"));
		if (list.contains("CUST_TYPE_ID"))
			current.setCustTypeId(rs.getLong("CUST_TYPE_ID"));
		if (list.contains("CUST_ADDR"))
			current.setCustAddr(rs.getString ("CUST_ADDR"));
		if (list.contains("CUST_PHONE"))
			current.setCustPhone(rs.getString ("CUST_PHONE"));
		if (list.contains("CUST_TEL"))
			current.setCustTel(rs.getString ("CUST_TEL"));
		if (list.contains("CUST_CONT_NAME"))
			current.setCustContName(rs.getString ("CUST_CONT_NAME"));
		if (list.contains("CUST_STAT"))
			current.setCustStat(rs.getInt("CUST_STAT"));
		if (list.contains("CUST_AREA"))
			current.setCustArea(rs.getString ("CUST_AREA"));
		if (list.contains("CUST_FAX"))
			current.setCustFax(rs.getString ("CUST_FAX"));
		if (list.contains("CUST_MAIL_BOX"))
			current.setCustMailBox(rs.getString ("CUST_MAIL_BOX"));
		if (list.contains("CUST_EMAIL"))
			current.setCustEmail(rs.getString ("CUST_EMAIL"));
		if (list.contains("CUST_COUN"))
			current.setCustCoun(rs.getString ("CUST_COUN"));
		if (list.contains("CUST_CONT_JOB"))
			current.setCustContJob(rs.getString ("CUST_CONT_JOB"));
		if (list.contains("CUST_COMP_DESC"))
			current.setCustCompDesc(rs.getString ("CUST_COMP_DESC"));
		if (list.contains("CUST_BANK"))
			current.setCustBank(rs.getString ("CUST_BANK"));
		if (list.contains("CUST_BANK_ACCO"))
			current.setCustBankAcco(rs.getString ("CUST_BANK_ACCO"));
		if (list.contains("CUST_SW_DJH"))
			current.setCustSwDjh(rs.getString ("CUST_SW_DJH"));
		if (list.contains("CUST_SF_CRED_MANA"))
			current.setCustSfCredMana(rs.getInt("CUST_SF_CRED_MANA"));
		if (list.contains("CUST_SALE_MODE"))
			current.setCustSaleMode(rs.getString ("CUST_SALE_MODE"));
		if (list.contains("CUST_INCR_RATE"))
			current.setCustIncrRate(rs.getBigDecimal("CUST_INCR_RATE"));
		if (list.contains("CUST_CITY"))
			current.setCustCity(rs.getString ("CUST_CITY"));
		if (list.contains("CUST_PROV"))
			current.setCustProv(rs.getString ("CUST_PROV"));
		if (list.contains("CUST_HOME_PAGE"))
			current.setCustHomePage(rs.getString ("CUST_HOME_PAGE"));
		if (list.contains("CURR_ID"))
			current.setCurrId(rs.getLong("CURR_ID"));
		if (list.contains("CUST_CURR_NAME"))
			current.setCustCurrName(rs.getString ("CUST_CURR_NAME"));
		if (list.contains("CUST_PAYM_TYPE"))
			current.setCustPaymType(rs.getInt("CUST_PAYM_TYPE"));
		if (list.contains("CUST_CORP_REPR"))
			current.setCustCorpRepr(rs.getString ("CUST_CORP_REPR"));
		if (list.contains("CUST_CUST_CODE"))
			current.setCustCustCode(rs.getString ("CUST_CUST_CODE"));
		if (list.contains("CUST_DESE_EXPE_TAXA_CODE"))
			current.setCustDeseExpeTaxaCode(rs.getString ("CUST_DESE_EXPE_TAXA_CODE"));
		if (list.contains("CUST_ADVA_ACCO_CODE"))
			current.setCustAdvaAccoCode(rs.getString ("CUST_ADVA_ACCO_CODE"));
		if (list.contains("CUST_PAY_EXPE_TAXA_CODE"))
			current.setCustPayExpeTaxaCode(rs.getString ("CUST_PAY_EXPE_TAXA_CODE"));
		if (list.contains("CUST_PREF_POLI"))
			current.setCustPrefPoli(rs.getString ("CUST_PREF_POLI"));
		if (list.contains("CUST_BRAN_PASS_DEPT"))
			current.setCustBranPassDept(rs.getLong("CUST_BRAN_PASS_DEPT"));
		if (list.contains("CUST_SPEC_SALE"))
			current.setCustSpecSale(rs.getLong("CUST_SPEC_SALE"));
		if (list.contains("CUST_LAST_TRAD_DATE"))
			current.setCustLastTradDate(rs.getLong("CUST_LAST_TRAD_DATE"));
		if (list.contains("CUST_LAST_TRAD_MONE"))
			current.setCustLastTradMone(rs.getBigDecimal("CUST_LAST_TRAD_MONE"));
		if (list.contains("CUST_LAST_DESE_SK_DATE"))
			current.setCustLastDeseSkDate(rs.getLong("CUST_LAST_DESE_SK_DATE"));
		if (list.contains("CUST_LAST_DESE_MONE"))
			current.setCustLastDeseMone(rs.getBigDecimal("CUST_LAST_DESE_MONE"));
		if (list.contains("CUST_MAX_TRAD_MONE"))
			current.setCustMaxTradMone(rs.getBigDecimal("CUST_MAX_TRAD_MONE"));
		if (list.contains("CUST_MIN_DESE_RATE"))
			current.setCustMinDeseRate(rs.getBigDecimal("CUST_MIN_DESE_RATE"));
		if (list.contains("CUST_MIN_SUBS_RATE"))
			current.setCustMinSubsRate(rs.getBigDecimal("CUST_MIN_SUBS_RATE"));
		if (list.contains("CUST_CZ_LEVEL"))
			current.setCustCzLevel(rs.getInt("CUST_CZ_LEVEL"));
		if (list.contains("CUST_ACCO_TOTAL"))
			current.setCustAccoTotal(rs.getBigDecimal("CUST_ACCO_TOTAL"));
		if (list.contains("CUST_PAYM_TOTAL"))
			current.setCustPaymTotal(rs.getBigDecimal("CUST_PAYM_TOTAL"));
		if (list.contains("CUST_FORG_TOTAL"))
			current.setCustForgTotal(rs.getBigDecimal("CUST_FORG_TOTAL"));
		if (list.contains("CUST_CRED"))
			current.setCustCred(rs.getBigDecimal("CUST_CRED"));
		if (list.contains("CUST_SPEC_KEFU"))
			current.setCustSpecKefu(rs.getLong("CUST_SPEC_KEFU"));
		if (list.contains("CUST_SF_CW_INIT"))
			current.setCustSfCwInit(rs.getInt("CUST_SF_CW_INIT"));
		return current;
	}
	
	/**
	 * 从客户ID获取客户
	 */
	public Custom getCustomByID(long custId) throws DaoException{
		logger.debug("Custom getCustomByID("+custId+") - run"); 
		return (Custom)queryObject("SELECT CUST_ID,CUST_NAME,CUST_TYPE_ID,CUST_ADDR,CUST_PHONE,CUST_TEL,CUST_CONT_NAME,CUST_STAT,CUST_AREA,CUST_FAX,CUST_MAIL_BOX,CUST_EMAIL,CUST_COUN,CUST_CONT_JOB,CUST_COMP_DESC,CUST_BANK,CUST_BANK_ACCO,CUST_SW_DJH,CUST_SF_CRED_MANA,CUST_SALE_MODE,CUST_INCR_RATE,CUST_CITY,CUST_PROV,CUST_HOME_PAGE,CURR_ID,CUST_CURR_NAME,CUST_PAYM_TYPE,CUST_CORP_REPR,CUST_CUST_CODE,CUST_DESE_EXPE_TAXA_CODE,CUST_ADVA_ACCO_CODE,CUST_PAY_EXPE_TAXA_CODE,CUST_PREF_POLI,CUST_BRAN_PASS_DEPT,CUST_SPEC_SALE,CUST_LAST_TRAD_DATE,CUST_LAST_TRAD_MONE,CUST_LAST_DESE_SK_DATE,CUST_LAST_DESE_MONE,CUST_MAX_TRAD_MONE,CUST_MIN_DESE_RATE,CUST_MIN_SUBS_RATE,CUST_CZ_LEVEL,CUST_ACCO_TOTAL,CUST_PAYM_TOTAL,CUST_FORG_TOTAL,CUST_CRED,CUST_SPEC_KEFU,CUST_SF_CW_INIT  FROM XT_CUSTOM_T WHERE CUST_ID=" + custId);
	}
	
	/**
	 * 添加客户
	 */
	public int addCustom(Custom obj) throws DaoException {
		logger.debug("addCustom(Custom) - start"); 

		int result = 0;
		List paramList = new ArrayList();
		
		long custId= querySequence("SEQ_XT_CUSTOM_T");
		paramList.add(custId);		
		paramList.add(obj.getCustName().trim());
		paramList.add(obj.getCustTypeId());
		paramList.add(obj.getCustAddr().trim());
		paramList.add(obj.getCustPhone().trim());
		paramList.add(obj.getCustTel().trim());
		paramList.add(obj.getCustContName().trim());
		paramList.add(obj.getCustStat());
		paramList.add(obj.getCustArea().trim());
		paramList.add(obj.getCustFax().trim());
		paramList.add(obj.getCustMailBox().trim());
		paramList.add(obj.getCustEmail().trim());
		paramList.add(obj.getCustCoun().trim());
		paramList.add(obj.getCustContJob().trim());
		paramList.add(obj.getCustCompDesc().trim());
		paramList.add(obj.getCustBank().trim());
		paramList.add(obj.getCustBankAcco().trim());
		paramList.add(obj.getCustSwDjh().trim());
		paramList.add(obj.getCustSfCredMana());
		paramList.add(obj.getCustSaleMode().trim());
		paramList.add(obj.getCustIncrRate());
		paramList.add(obj.getCustCity().trim());
		paramList.add(obj.getCustProv().trim());
		paramList.add(obj.getCustHomePage().trim());
		paramList.add(obj.getCurrId());
		paramList.add(obj.getCustCurrName().trim());
		paramList.add(obj.getCustPaymType());
		paramList.add(obj.getCustCorpRepr().trim());
		paramList.add(obj.getCustCustCode().trim());
		paramList.add(obj.getCustDeseExpeTaxaCode().trim());
		paramList.add(obj.getCustAdvaAccoCode().trim());
		paramList.add(obj.getCustPayExpeTaxaCode().trim());
		paramList.add(obj.getCustPrefPoli().trim());
		paramList.add(obj.getCustBranPassDept());
		paramList.add(obj.getCustSpecSale());
		paramList.add(obj.getCustLastTradDate());
		paramList.add(obj.getCustLastTradMone());
		paramList.add(obj.getCustLastDeseSkDate());
		paramList.add(obj.getCustLastDeseMone());
		paramList.add(obj.getCustMaxTradMone());
		paramList.add(obj.getCustMinDeseRate());
		paramList.add(obj.getCustMinSubsRate());
		paramList.add(obj.getCustCzLevel());
		paramList.add(obj.getCustAccoTotal());
		paramList.add(obj.getCustPaymTotal());
		paramList.add(obj.getCustForgTotal());
		paramList.add(obj.getCustCred());
		paramList.add(obj.getCustSpecKefu());
		paramList.add(obj.getCustSfCwInit());
		
		String sql = "INSERT INTO XT_CUSTOM_T (CUST_ID,CUST_NAME,CUST_TYPE_ID,CUST_ADDR,CUST_PHONE,CUST_TEL,CUST_CONT_NAME,CUST_STAT,CUST_AREA,CUST_FAX,CUST_MAIL_BOX,CUST_EMAIL,CUST_COUN,CUST_CONT_JOB,CUST_COMP_DESC,CUST_BANK,CUST_BANK_ACCO,CUST_SW_DJH,CUST_SF_CRED_MANA,CUST_SALE_MODE,CUST_INCR_RATE,CUST_CITY,CUST_PROV,CUST_HOME_PAGE,CURR_ID,CUST_CURR_NAME,CUST_PAYM_TYPE,CUST_CORP_REPR,CUST_CUST_CODE,CUST_DESE_EXPE_TAXA_CODE,CUST_ADVA_ACCO_CODE,CUST_PAY_EXPE_TAXA_CODE,CUST_PREF_POLI,CUST_BRAN_PASS_DEPT,CUST_SPEC_SALE,CUST_LAST_TRAD_DATE,CUST_LAST_TRAD_MONE,CUST_LAST_DESE_SK_DATE,CUST_LAST_DESE_MONE,CUST_MAX_TRAD_MONE,CUST_MIN_DESE_RATE,CUST_MIN_SUBS_RATE,CUST_CZ_LEVEL,CUST_ACCO_TOTAL,CUST_PAYM_TOTAL,CUST_FORG_TOTAL,CUST_CRED,CUST_SPEC_KEFU,CUST_SF_CW_INIT) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
		result = dao.prepareUpdate(sql, paramList);
		if (result != 1){
			throw new DaoException("添加客户失败!\n 客户信息" + BeanUtil.getBeanLog(obj));
		}

		logger.debug("addCustom(Custom) - end"); 
		return result;
	}
	
	/**
	 * 删除客户
	 */
	public int delCustom(Custom obj) throws DaoException {
        logger.debug("delCustom(Custom) - start"); 
		
		int result = 0;
		List paramList = new ArrayList();
		
		paramList.add(obj.getCustId());
	 
		String sql = "DELETE FROM XT_CUSTOM_T WHERE CUST_ID=?";
		result = dao.prepareUpdate(sql, paramList);
		if (result != 1){
			throw new DaoException("删除客户失败!\n 客户信息" + BeanUtil.getBeanLog(obj));
		}
        
		logger.debug("delCustom(Custom) - end"); 
		return result;
	}
	
	/**
	 * 返回客户的总数
	 */
	public int getTotalCustoms() throws DaoException {
		logger.debug("getTotalCustoms()  - run"); 
		
		String sql = "SELECT COUNT(1) FROM XT_CUSTOM_T";
		return queryInt(sql);
	}


	/**
	 * 查询客户并排序
	 */
	public List getCustoms(String property, String sortOrder,int limit, int offset) throws DaoException {
		logger.debug("List getCustoms  ("+property+","+ sortOrder+","+ limit+","+  offset+")- run"); 
		
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT CUST_ID,CUST_NAME,CUST_TYPE_ID,CUST_ADDR,CUST_PHONE,CUST_TEL,CUST_CONT_NAME,CUST_STAT,CUST_AREA,CUST_FAX,CUST_MAIL_BOX,CUST_EMAIL,CUST_COUN,CUST_CONT_JOB,CUST_COMP_DESC,CUST_BANK,CUST_BANK_ACCO,CUST_SW_DJH,CUST_SF_CRED_MANA,CUST_SALE_MODE,CUST_INCR_RATE,CUST_CITY,CUST_PROV,CUST_HOME_PAGE,CURR_ID,CUST_CURR_NAME,CUST_PAYM_TYPE,CUST_CORP_REPR,CUST_CUST_CODE,CUST_DESE_EXPE_TAXA_CODE,CUST_ADVA_ACCO_CODE,CUST_PAY_EXPE_TAXA_CODE,CUST_PREF_POLI,CUST_BRAN_PASS_DEPT,CUST_SPEC_SALE,CUST_LAST_TRAD_DATE,CUST_LAST_TRAD_MONE,CUST_LAST_DESE_SK_DATE,CUST_LAST_DESE_MONE,CUST_MAX_TRAD_MONE,CUST_MIN_DESE_RATE,CUST_MIN_SUBS_RATE,CUST_CZ_LEVEL,CUST_ACCO_TOTAL,CUST_PAYM_TOTAL,CUST_FORG_TOTAL,CUST_CRED,CUST_SPEC_KEFU,CUST_SF_CW_INIT  FROM XT_CUSTOM_T");
		if (property != null && sortOrder != null) {
			sb.append(" ORDER BY ");
			sb.append(" ");
			sb.append(property);
			sb.append(" ");
			sb.append(sortOrder);
		} else{
			sb.append(" ORDER BY CUST_ID DESC ");
		}
		
		logger.debug("List getCustoms(String property, String sortOrder,int limit, int offset)- end"); 
		return queryList(sb.toString(),limit,offset);
	}
	
	/**
	 * 更新客户
	 */
	public int updateCustom(Custom obj) throws DaoException {
		logger.debug("updateCustom(Custom) - start"); 
		
		int result = 0;
		List paramList = new ArrayList();
		
		paramList.add(obj.getCustName().trim());
		paramList.add(obj.getCustTypeId());
		paramList.add(obj.getCustAddr().trim());
		paramList.add(obj.getCustPhone().trim());
		paramList.add(obj.getCustTel().trim());
		paramList.add(obj.getCustContName().trim());
		paramList.add(obj.getCustStat());
		paramList.add(obj.getCustArea().trim());
		paramList.add(obj.getCustFax().trim());
		paramList.add(obj.getCustMailBox().trim());
		paramList.add(obj.getCustEmail().trim());
		paramList.add(obj.getCustCoun().trim());
		paramList.add(obj.getCustContJob().trim());
		paramList.add(obj.getCustCompDesc().trim());
		paramList.add(obj.getCustBank().trim());
		paramList.add(obj.getCustBankAcco().trim());
		paramList.add(obj.getCustSwDjh().trim());
		paramList.add(obj.getCustSfCredMana());
		paramList.add(obj.getCustSaleMode().trim());
		paramList.add(obj.getCustIncrRate());
		paramList.add(obj.getCustCity().trim());
		paramList.add(obj.getCustProv().trim());
		paramList.add(obj.getCustHomePage().trim());
		paramList.add(obj.getCurrId());
		paramList.add(obj.getCustCurrName().trim());
		paramList.add(obj.getCustPaymType());
		paramList.add(obj.getCustCorpRepr().trim());
		paramList.add(obj.getCustCustCode().trim());
		paramList.add(obj.getCustDeseExpeTaxaCode().trim());
		paramList.add(obj.getCustAdvaAccoCode().trim());
		paramList.add(obj.getCustPayExpeTaxaCode().trim());
		paramList.add(obj.getCustPrefPoli().trim());
		paramList.add(obj.getCustBranPassDept());
		paramList.add(obj.getCustSpecSale());
		paramList.add(obj.getCustLastTradDate());
		paramList.add(obj.getCustLastTradMone());
		paramList.add(obj.getCustLastDeseSkDate());
		paramList.add(obj.getCustLastDeseMone());
		paramList.add(obj.getCustMaxTradMone());
		paramList.add(obj.getCustMinDeseRate());
		paramList.add(obj.getCustMinSubsRate());
		paramList.add(obj.getCustCzLevel());
		paramList.add(obj.getCustAccoTotal());
		paramList.add(obj.getCustPaymTotal());
		paramList.add(obj.getCustForgTotal());
		paramList.add(obj.getCustCred());
		paramList.add(obj.getCustSpecKefu());
		paramList.add(obj.getCustSfCwInit());
		
		paramList.add(obj.getCustId());
		
		String sql = "UPDATE XT_CUSTOM_T SET CUST_NAME=?,CUST_TYPE_ID=?,CUST_ADDR=?,CUST_PHONE=?,CUST_TEL=?,CUST_CONT_NAME=?,CUST_STAT=?,CUST_AREA=?,CUST_FAX=?,CUST_MAIL_BOX=?,CUST_EMAIL=?,CUST_COUN=?,CUST_CONT_JOB=?,CUST_COMP_DESC=?,CUST_BANK=?,CUST_BANK_ACCO=?,CUST_SW_DJH=?,CUST_SF_CRED_MANA=?,CUST_SALE_MODE=?,CUST_INCR_RATE=?,CUST_CITY=?,CUST_PROV=?,CUST_HOME_PAGE=?,CURR_ID=?,CUST_CURR_NAME=?,CUST_PAYM_TYPE=?,CUST_CORP_REPR=?,CUST_CUST_CODE=?,CUST_DESE_EXPE_TAXA_CODE=?,CUST_ADVA_ACCO_CODE=?,CUST_PAY_EXPE_TAXA_CODE=?,CUST_PREF_POLI=?,CUST_BRAN_PASS_DEPT=?,CUST_SPEC_SALE=?,CUST_LAST_TRAD_DATE=?,CUST_LAST_TRAD_MONE=?,CUST_LAST_DESE_SK_DATE=?,CUST_LAST_DESE_MONE=?,CUST_MAX_TRAD_MONE=?,CUST_MIN_DESE_RATE=?,CUST_MIN_SUBS_RATE=?,CUST_CZ_LEVEL=?,CUST_ACCO_TOTAL=?,CUST_PAYM_TOTAL=?,CUST_FORG_TOTAL=?,CUST_CRED=?,CUST_SPEC_KEFU=?,CUST_SF_CW_INIT=?  WHERE CUST_ID=?";
		result = dao.prepareUpdate(sql, paramList);
		if (result != 1){
			throw new DaoException("更新客户失败!\n 客户信息" + BeanUtil.getBeanLog(obj));
		}
		
		logger.debug("updateCustom(Custom) - end"); 
		return result;
	}
}
